package com.agileai.bpm.controller.emulation;

import java.util.ArrayList;
import java.util.List;

import com.agileai.bpm.bizmoduler.emulation.TaskListQuery;
import com.agileai.bpm.domain.running.WorkItem;
import com.agileai.bpm.service.running.ProcessHandle;
import com.agileai.bpm.webservice.BPMService;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.QueryModelListHandler;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

public class WfEmulationTaskListQueryHandler extends QueryModelListHandler{
	public WfEmulationTaskListQueryHandler(){
		super();
		this.detailHandlerClazz = TaskApproveFrameHandler.class;
		this.serviceId = buildServiceId(TaskListQuery.class);
	}
	public ViewRenderer prepareDisplay(DataParam param){
		try {
			mergeParam(param);
			initParameters(param);
			this.setAttributes(param);
			List<DataRow> rsList = new ArrayList<DataRow>();
			User user = (User)this.getUser();
			String userId = param.get("userId");
			if (StringUtil.isNullOrEmpty(userId) && user.isAdmin()){
				rsList = getService().findRecords(param);			
			}
			else{
				BPMService bpmService = ProcessHelper.instance().getBPMService();
				ProcessHandle processHandle = (ProcessHandle)this.lookupService("processHandle");
				String userCode = processHandle.findUserCode(userId);
				List<WorkItem> taskItemList = bpmService.findWorkItems(userCode);
				processRecords(taskItemList, rsList);
			}
			this.setRsList(rsList);
			processPageAttributes(param);
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new LocalRenderer(getPage());
	}
	public ViewRenderer doRedirectAction(DataParam param){
		storeParam(param);
		String url = param.get("bizFormURL");
		url = url+"&comeFromTaskList=true";
		return new RedirectRenderer(url);
	}
	private void processRecords(List<WorkItem> taskItemList,List<DataRow> rsList){
		if (taskItemList != null){
			for (int i=0;i < taskItemList.size();i++){
				WorkItem workItem = taskItemList.get(i);
				DataRow row = new DataRow();
				row.put("WFIP_ID",workItem.getProcInstanceId());
				row.put("WFP_ID",workItem.getProcessId());
				row.put("WFA_CODE",workItem.getActivityCode());
				row.put("WFIP_BUSINESS_ID",workItem.getBusinessId());
				row.put("WFIA_BIZ_URL",workItem.getBusinessURL());
				row.put("WFP_NAME",workItem.getProcessName());
				row.put("WFA_NAME",workItem.getActivityName());
				row.put("WFIP_TITLE",workItem.getTitle());
				row.put("WFIP_STATUS",workItem.getProcInstanceState());
				row.put("WFIP_TITLE",workItem.getTitle());
				row.put("WFIP_OPERATER_NAME",workItem.getSubmiterName());
				row.put("WFIP_OPERATER_TIME",DateUtil.getDateTime(workItem.getSubmiteTime()));
				rsList.add(row);
			}			
		}
	}
	
	protected void processPageAttributes(DataParam param) {
		
	}
	protected void initParameters(DataParam param) {
	}
	protected TaskListQuery getService() {
		return (TaskListQuery)this.lookupService(this.getServiceId());
	}
}	
